package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class RegisterActivity extends AppCompatActivity {

    private Button mbtnret,mBtnRes;
    private EditText rid,rpass,rrepeat,rcode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mbtnret=findViewById(R.id.btn_r_return);
        mBtnRes=findViewById(R.id.btn_r_register);
        rid=findViewById(R.id.et_r_1);
        rpass=findViewById(R.id.et_r_2);
        rrepeat=findViewById(R.id.et_r_3);
        rcode=findViewById(R.id.et_r_4);
        String back="注册成功，来登录吧!";
        String fail1="该用户名已被注册";
        String fail2="无效的邀请码";
        String fail0="请输入完整信息";
        String fail00="密码和确认密码应一致";
        mbtnret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBtnRes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id=rid.getText().toString();
                String pass=rpass.getText().toString();
                String repeat=rrepeat.getText().toString();
                String code=rcode.getText().toString();
                Toast toastfail;
                Intent intent=null;
                if(id.equals("")||pass.equals("")||repeat.equals("")||code.equals("")){
                    toastfail=Toast.makeText(getApplicationContext(),fail0,Toast.LENGTH_SHORT);
                    toastfail.setGravity(Gravity.CENTER,0,0);
                    toastfail.show();
                }
                else if(!pass.equals(repeat)){
                    toastfail=Toast.makeText(getApplicationContext(),fail00,Toast.LENGTH_SHORT);
                    toastfail.setGravity(Gravity.CENTER,0,0);
                    toastfail.show();
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String url="http://10.128.48.203:8000/register?work_number="+id+"&password="+pass+"&code="+code;
                            Toast toastfail;
                            BufferedReader buf=null;
                            String result;
                            HttpURLConnection logincon = null;
                            try {
                                URL requesturl= new URL(url);
                                logincon=(HttpURLConnection)requesturl.openConnection();
                                logincon.setRequestMethod("GET");
                                logincon.setReadTimeout(5000);
                                logincon.connect();

                                if(logincon.getResponseCode()==200){
                                    //获取二进制字节流
                                    InputStream inputs=logincon.getInputStream();

                                    //将二进制流包装
                                    buf =new BufferedReader(new InputStreamReader(inputs));

                                    //读取字符串
                                    String line;
                                    StringBuilder builder = new StringBuilder();
                                    while((line=buf.readLine())!=null){
                                        builder.append(line);
                                        builder.append("\n");
                                    }
                                    result=builder.toString();
                                    JSONObject jsonObject = new JSONObject(result);
                                    String errno=jsonObject.optString("errno");
                                    switch (errno) {
                                        case "0":
                                            Toast.makeText(getApplicationContext(),back,Toast.LENGTH_SHORT).show();
                                            finish();
                                            break;
                                        case "1":
                                            toastfail=Toast.makeText(getApplicationContext(),fail1,Toast.LENGTH_SHORT);
                                            toastfail.setGravity(Gravity.CENTER,0,0);
                                            toastfail.show();
                                            break;
                                        case "2":
                                            toastfail=Toast.makeText(getApplicationContext(),fail2,Toast.LENGTH_SHORT);
                                            toastfail.setGravity(Gravity.CENTER,0,0);
                                            toastfail.show();
                                            break;
                                    }
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });
    }
}